# # 作业1：IP存储
# # 方法一：
# import random
# # ipstxt()创建ips.txt
# def ipstxt(filename):
#     ips = ['172.25.254.' + str(i) for i in range(1,255)]
#     with open(filename,'a+') as file:
#         for i in range(12000):
#             file.write(random.sample(ips,1)[0] + '\n')
# # 调用ipstxt()
# ipstxt('ips.txt')
# # frequncy(filename,count=10)读取次数
# def frequncy(filename,count=10):
#     # 创建IP出现次数的字典
#     ip_dict = {}
#     top = "IP榜单TOP10"
#     # 创建TOP10字典
#     TOP10_lst = {}
#     j = 0
#     with open(filename) as file:
#         for ip in file:
#             # strip保留ip，去除其他字符
#             ip = ip.strip()
#             if ip in ip_dict:
#                 ip_dict[ip] += 1
#             else:
#                 ip_dict[ip] = 1
#     sorted_ip = sorted(ip_dict.items(), key=lambda x: x[1], reverse=True)[:count]
#     print(top.center(66,"*"))
#     for i in sorted_ip:
#         j += 1
#         print(f"出现次数第{j}的IP地址以及对应的次数分别为{i}\n")
#     return "*************************************************"
# print(frequncy('ips.txt'))

# # 方法二：
# import random
# # ipstxt()创建ips.txt
# def ipstxt(filename):
#     ips = ['172.25.254.' + str(i) for i in range(1,255)]
#     with open(filename,'a+') as file:
#         for i in range(12000):
#             file.write(random.sample(ips,1)[0] + '\n')
#
# # 调用ipstxt()
# ipstxt('ips.txt')
#
# # frequncy(filename,count=10)读取次数
# def frequncy(filename):
#     # 创建IP出现次数的字典
#     ip_dict = {}
#     # 创建TOP10字典
#     TOP10_lst = {}
#     with open(filename) as file:
#         for ip in file:
#             # strip保留ip，去除其他字符
#             ip = ip.strip()
#             if ip in ip_dict:
#                 ip_dict[ip] += 1
#             else:
#                 ip_dict[ip] = 1
#     lst = list(ip_dict.values())
#     lst.sort()
#     top_lst = lst[0:10]
#     # 通过所有ip地址出现次数与前十次数是否相等来取出对应的IP地址
#     for i in ip_dict.items():
#         if i[1] in top_lst:
#             # 通过value返回出key，并生成字典TOP10_lst
#             TOP10_lst[i[0]] = i[1]
#     for i, j in TOP10_lst.items():
#         print(f"出现次数前十的IP地址{i[0:-1]}以及对应的次数分别为{j}\n")
#     return "*************************************************"
# print(frequncy('ips.txt'))


# # 作业二：瓦尔登湖
# 打开文本

with open('Walden.txt',encoding="utf-8") as file:
    # 将文本内容全部输出为小写
    frequency = {}
    for text_string in file:
        text_string = text_string[0:-1]
        text_string.lower()
        for i in "1234567890,:;-!":
            text_string = text_string.replace(i," ")
        # text_string.replace("0"," ").replace("1"," ").replace("2"," ").replace("3"," ").replace("4"," ").replace("5"," ").replace("6"," ") \
        # .replace("7", " ").replace("8"," ").replace("9"," ").replace("!"," ").replace("."," ").replace(","," ").replace("?"," ").replace("'"," ") \
        # .replace("-", " ").replace(";"," ").replace(":"," ").replace("--"," ")
        str1 = text_string.split(' ')
        for i in str1:
            if i.isalpha() and i in frequency:
                frequency[i] += 1
            elif i.isalpha() and i not in frequency:
                frequency[i] = 1
print(frequency)






